﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
using System.Data;
using System.Web.Mvc;
using webcspa.Models;

namespace webcspa.Controllers
{
    public class SPDataController : Controller
    {
        private const string connectionString = "Data Source=GORBUNOV-MS\\GORBUNOV;Initial Catalog=CSPA;Persist Security Info=True;User ID=sa;Password=vbif0192837465";

        [HttpGet]
        public ActionResult Run()
        {
            var result = new List<SPDataModel>();

            using (var connection = new SqlConnection(connectionString))
            {
                SqlCommand myCmd = new SqlCommand("p_REG_NODES", connection);
                myCmd.CommandType = CommandType.Text;

                myCmd.Parameters.Add(new SqlParameter("@shema_id", SqlDbType.Int));
                myCmd.Parameters["@shema_id"].Value = 0;

                myCmd.Parameters.Add(new SqlParameter("@regim_id", SqlDbType.Int));
                myCmd.Parameters["@regim_id"].Direction = ParameterDirection.ReturnValue;

                connection.Open();
                var reader = myCmd.ExecuteReader();

                while (reader.Read())
                {
                    var item = new SPDataModel
                                   {
                                      NODE = ReadInt(reader, 0),
                                      U = ReadDouble(reader, 1),
                                      FAZA = ReadDouble(reader, 2),
                                      PN = ReadDouble(reader, 3),
                                      QN = ReadDouble(reader, 4),
                                      PN_NOM = ReadDouble(reader, 5),
                                      QN_NOM = ReadDouble(reader, 6),
                                      CXH_TIP = ReadInt(reader, 7),
                                      SYN_PART_ID = ReadInt(reader, 8),
                                      TM_U = ReadDouble(reader, 9),
                                      TM_PN = ReadDouble(reader, 10),
                                      TM_QN = ReadDouble(reader, 11),
                                      TM_PG_SUM = ReadDouble(reader, 12),
                                      TM_QG_SUM = ReadDouble(reader, 13),
                                      ES_PG_SUM = ReadDouble(reader, 14),
                                      ES_PG_MIN = ReadDouble(reader, 15),
                                      ES_PG_MAX = ReadDouble(reader, 16),
                                      ES_QG_SUM = ReadDouble(reader, 17),
                                      ES_QG_MIN = ReadDouble(reader, 18),
                                      ES_QG_MAX = ReadDouble(reader, 19),
                                      LY = ReadDouble(reader, 20),
                                      TM_KONTROL_UST = ReadInt(reader, 21),
                                      QR_SH = ReadDouble(reader, 22),
                                      TM_PN_GR = ReadDouble(reader, 23),
                                      TM_QN_GR = ReadDouble(reader, 24),
                                      TM_PG_SUM_GR = ReadDouble(reader, 25),
                                      TM_QG_SUM_GR = ReadDouble(reader, 26),
                                      VES1 = ReadInt(reader, 27),
                                      P_SN = ReadDouble(reader, 28),
                                      Q_BTR = ReadDouble(reader, 29),
                                      Q_SN = ReadDouble(reader, 30),
                                      VES2 = ReadInt(reader, 31),
                                      NK = ReadDouble(reader, 32),
                                   };
                    result.Add(item);
                }
            }
            return View("SP_View", result.ToArray());
        }

        private double ReadDouble(SqlDataReader reader, int index)
        {
            var result = reader.GetSqlDouble(index);
            return result.IsNull ? -1 : result.Value;
        }

        private int ReadInt(SqlDataReader reader, int index)
        {
            var result = reader.GetSqlInt32(index);
            return result.IsNull ? -1 : result.Value;
        }

    }
}

